/*
 * Epoll.h
 *
 *  Created on: 2021年4月5日
 *      Author: san
 */

#ifndef EPOLL_H_
#define EPOLL_H_

#include <sys/epoll.h>
#include <memory>
#include <unordered_map>
#include <vector>
#include "Channel.h"
#include "HttpData.h"
#include "Timer.h"
namespace san {

class Epoll {
public:
	Epoll();
	~Epoll();
	void epoll_add(ChannelPtr request, int timeout);
	void epoll_mod(ChannelPtr request, int timeout);
	void epoll_del(ChannelPtr request);

	std::vector<std::shared_ptr<Channel> > poll();
	std::vector<std::shared_ptr<Channel> > getEventsRequest(int events_num);

	// 用于超时断开
	void add_timer(std::shared_ptr<Channel> request_data, int timeout);

	int getEpollFd() { return epollFd_; }
	void handleExpired();

private:
	static const int MAXFDS = 100000;
	int epollFd_;
	std::vector<epoll_event> events_;

	//
	std::shared_ptr<Channel> fd2chan_[MAXFDS];

	// 用于描述符和http请求的对应
	std::shared_ptr<HttpData> fd2http_[MAXFDS];

	TimerManager timerManager_;
};

} /* namespace san */

#endif /* EPOLL_H_ */
